<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;

class TelecomController extends BaseController
{
  private $log;
  public function __construct()
  {
    $this->log = new Logger('TelecomPush');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/TelecomPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    // var_dump(1111111111);
    // $getData = '{"logisticsCompanies":"顺丰快递","sign":"64B92D7B8B8D027CCB52D5574068C3F9","phoneNum":"F95662BD5703BBDB8AA03793A08E8EDA","ordId":"Hk_20220521170443374284738314","orderStatusDescribe":"订单完成","orderState":"D1002","requestTime":1653235229000,"areaCode":"0399","balanceOne":"20000.0","identityId":"B0E29D818D5B4EA9848EA75EB4F4D91DFEF0D36AED32723F0DCF2577C22A7D1B","name":"029CE5B6800B31E3DECAC5493CE768E7","uim":"5674320039F2CA563E9C5FCC112B970F9411186656B275C747F1B7262C45D3DF","logisticsNo":"4BED77E946003D610086CEA5B264535F","state":"100000","describe":"激活成功","stateDate":1653216306000}';
    // $getData = '{"logisticsCompanies":"京东物流","sign":"D60480C05C2CF5AD412599253C6CD6EA","phoneNum":"166C8E18789555435CC9E79FE8ACA0BC","ordId":"HK_6918848277138642336-2","orderStatusDescribe":"订单完成","orderState":"K1009","requestTime":1685351213000,"areaCode":"0393","identityId":"2ED73882B5A4CCEBAA447B1660DBE4B9AA82BE3E107A93C95AFDC7F569AA58A1","name":"B300645E07DD6DA267F4A9A463387D65","uim":"19B6F9AB3771F0F36698594653FEC69B85C19260543D0D76630653C348C3D5BF","logisticsNo":"B430AE06EE7285E86BED2B60FD89EDA5","describe":"无订单推送信息","stateDate":1685350604000}';
    // $getData = '{"logisticsCompanies":"京东物流","sign":"ADE4E79EDB9E20F48447C4EB47DC2734","phoneNum":"A5FA142155EA76D49B96E185E6E9275C","ordId":"HK_6918976087443838629","orderStatusDescribe":"订单完成","orderState":"K1009","requestTime":1685678332000,"areaCode":"0379","identityId":"ECC37EB1AAA876B284CB2C3E2694F2D48A074AD0FBAED43BB218331A535A8199","name":"1ECB80232C1376F475FC1AABF684699D","uim":"76BCF68BEAEE349794C2DE0DED509EE0E14B23D73174BECC5E61234FBF3EC411","logisticsNo":"A4C7A6D507422A43AE60A10F967DC5CD","describe":"无订单推送信息","stateDate":1685677714000}';
    // $getData = '{"logisticsCompanies":"京东物流","sign":"CD77B9735B84FAC2B385939F27AF229D","phoneNum":"A5FA142155EA76D49B96E185E6E9275C","ordId":"HK_6918976087443838629","orderStatusDescribe":"订单完成","orderState":"K1009","requestTime":1685678864000,"areaCode":"0379","balanceOne":"","identityId":"ECC37EB1AAA876B284CB2C3E2694F2D48A074AD0FBAED43BB218331A535A8199","name":"1ECB80232C1376F475FC1AABF684699D","uim":"76BCF68BEAEE349794C2DE0DED509EE0E14B23D73174BECC5E61234FBF3EC411","logisticsNo":"A4C7A6D507422A43AE60A10F967DC5CD","describe":"待激活","stateDate":1685677806000}';
    // 数据处理  
    if (empty($getData)) {
      return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
    }
    $getData = json_decode($getData, true);
    if (empty($getData)) {
      return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
    }
    $statusData = [
      'D1002' => '40', //已激活
      'A1003' => '10', // 待处理
      'K1008' => '11', //代发货
      'K1009' => '20', // 已发货/代签收
      'Q1003' => '51', // 订单取消
      'D1001' => '30', // 已签收
    ];
    // var_dump($getData,999999999);
    // 将数据格式 修改成固定的数据格式
    $editData = [
      'status' => !empty($statusData[$getData['orderState']]) ? $statusData[$getData['orderState']] : '50',
      'express_company' => !empty($getData['logisticsCompanies'])?$getData['logisticsCompanies']:'',
      'express_number' => !empty($getData['logisticsNo'])?$getData['logisticsNo']:'',
      'three_msg' => $getData['describe'],
      'iccid' => $getData['phoneNum'],
      'first_status' => !empty($getData['balanceOne']) ? '1' : '0',
      'first_status_desc' => !empty($getData['balanceOne']) ? ($getData['balanceOne'] / 100) : '0',
      'imei' => !empty($getData['uim'])?$getData['uim']:''
    ];
    $where = ['ordersno' => $getData['ordId']];
    $model = new MainOrderRepostition();
    $descData = [
      'iccid' => $getData['phoneNum'],
      'imei'=>$getData['uim'],
    ];
    if(!empty($getData['logisticsNo'])){
      // $descData['express_number'] = $getData['logisticsNo'];
    }
    // var_dump($editData,1111111);die();
    // var_dump($where,55555555);
    $res = $model->followOrderStatus($where, $editData, $descData);
    $this->log->info('电信通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    }
    return json_encode(['responseCode' => 1, 'result' => '数据处理失败']);
  }
}
